﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;

namespace eTMS.Forms.Students
{
    public partial class FrmCapNhatDuLieuSinhVien : eTMS.Controls.BaseForm
    {
        public FrmCapNhatDuLieuSinhVien()
        {
            InitializeComponent();
        }
        private SinhVienController controller = new SinhVienController();
        private string _TableUpdate = "LyLichSinhVien";
        private string _ColumnUpdate = "DanToc_ID";
        private string _ColumnLink = "SinhVien_ID";
        private string _Column_ID = "SinhVien_ID";
        public string TableUpdate
        {
            set
            {
                _TableUpdate = value;
            }
        }
        public string ColumnUpdate
        {
            set
            {
                _ColumnUpdate = value;
            }
        }
        public string ColumnLink
        {
            set
            {
                _ColumnLink = value;
            }
        }
        public string Column_ID
        {
            set
            {
                _Column_ID = value;
            }
        }

        private string _HeaderText = "";
        private Control defaultControl;
        public void AddComboEditColumn(string ColumnText, string TableLookUp, string DisplayMember, string ValueMember)
        {
            AddComboEditColumn(ColumnText, TableLookUp, DisplayMember, ValueMember, true);
        }

        private string _DisplayMember;
        private string _ValueMember;
        private DataTable tblLookUp;
        public void AddComboEditColumn(string ColumnText, string TableLookUp, string DisplayMember, string ValueMember, bool IsAddSampleValue)
        {
            _ValueMember = ValueMember;
            _DisplayMember = DisplayMember;
            _HeaderText = ColumnText;
            DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
            col.HeaderText = ColumnText;
            col.DisplayMember = DisplayMember;
            col.ValueMember = ValueMember;
            col.DataPropertyName = _ColumnUpdate;
            col.ReadOnly = false;
            col.Name = "Col" + _ColumnUpdate;
            tblLookUp = controller.LoadDataTableLookUp(TableLookUp, ValueMember, DisplayMember);
            DataRow r = tblLookUp.NewRow();
            tblLookUp.Rows.InsertAt(r, 0);
            r[DisplayMember] = eTMS.Commons.Constants.APP_CHOOSE;
            col.DataSource = tblLookUp;
            col.Width = 120;
            sinhVienList1.AddColumn(col, 1);
            if (IsAddSampleValue)
            {
                eTMS.Controls.ComboBox comBo = new eTMS.Controls.ComboBox();
                comBo.DisplayMember = DisplayMember;
                comBo.ValueMember = ValueMember;
                comBo.Width = 120;
                comBo.DataSource = tblLookUp.Copy();
                defaultControl = comBo;
                eTMS.Controls.Label lbl = new eTMS.Controls.Label();
                lbl.Text = "Mặc định:";
                lbl.AutoSize = true;
                eTMS.Controls.Button btn = new eTMS.Controls.Button();
                btn.Text = "Đồng ý";
                btn.Width = 80;
                btn.Height = btnSearch.Height;
                btn.Click += new EventHandler(btn_Click);
                sinhVienList1.AddControlToBottomPanel(new Control[] { lbl, comBo, btn }, false);
            }
        }
        public void AddTextEditColumn(string ColumnText)
        {
            _HeaderText = ColumnText;
            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
            col.HeaderText = ColumnText;
            col.DataPropertyName = _ColumnUpdate;
            col.ReadOnly = false;
            col.Width = 100;
            col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            col.Name = "Col" + _ColumnUpdate;
            sinhVienList1.AddColumn(col, 1);
        }
        public void AddComBoNamHocHocKyFilter()
        {
            eTMS.Controls.Label lblNamHoc = new eTMS.Controls.Label();
            lblNamHoc.Text = "Năm học";
            lblNamHoc.Dock = DockStyle.Top;
            lblNamHoc.AutoSize = true;
            panelFilter.Visible = true;
            NamHocController.LoadAllNamHoc(false);
            eTMS.Controls.ComboBoxNamHoc cbNamHoc = new eTMS.Controls.ComboBoxNamHoc();
            cbNamHoc.Dock = DockStyle.Top;
            cbNamHoc.Name = "cbNamHoc";
            cbNamHoc.BindData();

            eTMS.Controls.Label lblHocKy = new eTMS.Controls.Label();
            lblHocKy.Text = "Học kỳ";
            lblHocKy.Dock = DockStyle.Top;
            lblHocKy.AutoSize = true;
            panelFilter.Visible = true;
            HocKyController.LoadAllHocKy(false);
            eTMS.Controls.ComboBoxHocKy cbHocKy = new eTMS.Controls.ComboBoxHocKy();
            cbHocKy.Dock = DockStyle.Top;
            cbHocKy.Name = "cbHocKy";
            cbHocKy.BindData();

            panelFilter.Controls.Add(cbHocKy);
            panelFilter.Controls.Add(lblHocKy);
            panelFilter.Controls.Add(cbNamHoc);
            panelFilter.Controls.Add(lblNamHoc);
        }
        void btn_Click(object sender, EventArgs e)
        {
            if (defaultControl is ComboBox)
            {
                controller.SetDefaultDataCommon(_ColumnUpdate, ((ComboBox)defaultControl).SelectedValue);
            }
            else if (defaultControl is TextBox)
            {
                controller.SetDefaultDataCommon(_ColumnUpdate, ((TextBox)defaultControl).Text);
            }
        }
        private string GetFilter()
        {
            string Filter = "";
            if (panelFilter.Controls.ContainsKey("cbNamHoc"))
            {
                ComboBox cb = (ComboBox)panelFilter.Controls["cbNamHoc"];
                if (cb.SelectedIndex >= 0)
                {
                    Filter += " AND " + _TableUpdate + ".NamHoc_ID=" + cb.SelectedValue.ToString();
                }
            }
            if (panelFilter.Controls.ContainsKey("cbHocKy"))
            {
                ComboBox cb = (ComboBox)panelFilter.Controls["cbHocKy"];
                if (cb.SelectedIndex >= 0)
                {
                    Filter += " AND " + _TableUpdate + ".HocKy_ID=" + cb.SelectedValue.ToString();
                }
            }
            if (Filter.Length > 0)
                Filter = Filter.Substring(4);
            return Filter;
        }
        private Dictionary<string, object> UpdateFields()
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            string Filter = "";
            if (panelFilter.Controls.ContainsKey("cbNamHoc"))
            {
                ComboBox cb = (ComboBox)panelFilter.Controls["cbNamHoc"];
                if (cb.SelectedIndex >= 0)
                {
                    dic.Add("namhoc_id", cb.SelectedValue);
                }
            }
            if (panelFilter.Controls.ContainsKey("cbHocKy"))
            {
                ComboBox cb = (ComboBox)panelFilter.Controls["cbHocKy"];
                if (cb.SelectedIndex >= 0)
                {
                    dic.Add("hocky_id", cb.SelectedValue);
                }
            }
            return dic;
        }
        private void DoSearchSinhVien()
        {
            controller.ResetObject();
            sinhVienInfoInput1.UpdateToModel();
            controller.LoadDataCommonUpdate(treeKhoaNganhKhoaHoc1.SelectedKhoa_ID, treeKhoaNganhKhoaHoc1.SelectedNganhHoc_ID, treeKhoaNganhKhoaHoc1.SelectedLop_ID, treeKhoaNganhKhoaHoc1.KhoaHoc_ID, _TableUpdate, _ColumnUpdate, _ColumnLink, _Column_ID, GetFilter());
            sinhVienList1.UpdateToScreen();
        }
        private void FrmCapNhatDuLieuSinhVien_Load(object sender, EventArgs e)
        {
            treeKhoaNganhKhoaHoc1.InitData();

            sinhVienInfoInput1.SetController(controller);
            sinhVienList1.SetController(controller);
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            DoSearchSinhVien();
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            controller.SaveDataCommon(_TableUpdate, _ColumnUpdate, _ColumnLink, _Column_ID, UpdateFields());
            ShowInfo("Bạn đã cập nhật dữ liệu thành công.");
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            DataTable table = controller.Table.Copy();
            table.Columns.Add("GIATRIIN");
            foreach (DataGridViewRow row in sinhVienList1.Rows)
            {
                table.ImportRow(((DataRowView)row.DataBoundItem).Row);
                table.Rows[table.Rows.Count - 1]["GIATRIIN"] = row.Cells["Col" + _ColumnUpdate].FormattedValue;
            }
            FrmAskForPrintCapNhatData frmAskForPrintCapNhatData=new FrmAskForPrintCapNhatData();
            frmAskForPrintCapNhatData.SetDataSource(table);
            frmAskForPrintCapNhatData.ReportName = _HeaderText;
            frmAskForPrintCapNhatData.ColName = _HeaderText;
            frmAskForPrintCapNhatData.ColumnUpdate = _ColumnUpdate;
            frmAskForPrintCapNhatData.SetDataSourceLookUp(tblLookUp,_DisplayMember,_ValueMember);
            frmAskForPrintCapNhatData.ShowDialog();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
